package game.server;

import java.awt.Point;

import game.Game;
import game.log.Logger;
import shared.io.game.EndGame;
import shared.io.game.Maze2;
import shared.io.game.Player;
import shared.io.messages.ErrorMessage;
import shared.io.messages.PlayerMessage;
import shared.io.messages.ServerMessage;
import shared.io.startup.NewGame;
import shared.io.startup.PlayerStartup;

/**
 * Manages incoming java.io.Serializable objects
 */
public class ObjectManager {

   private Server server;
   private Game game;
   
   public ObjectManager(Server server, Game game) {
      this.game = game;
      this.server = server;
   }
   
   /**
    * Manages incoming objects.
    * Object must implement java.io.Serializable and be known to the server.
    */
   public void manageObject(Object object, ClientSession session) {
      synchronized (game) {
         if (object instanceof java.io.Serializable) {
            if (object instanceof Point) {
               Point point = (Point) object;
               if (game.setPlayerLocation(session.getPlayer(), point) != null) {
                  server.sendObjectToSessions(game.getGameState(), session);
               } else {
                  session.sendObject(new ErrorMessage("You cannot walk on a wall."));
               }
               if (game.isExit(point)) {
                  server.sendObjectToSessions(new ServerMessage(session.getPlayer().getName() + " WON!"));
                  server.sendObjectToSessions(new EndGame(session.getPlayer()), session);
                  game.newGame();
                  server.sendObjectToSessions(new NewGame(new Maze2(
                                                                  game.getMaze().getMap(),
                                                                  game.getMaze().getLights()),
                                                                  game.getGameState()));
               }
               return;
            } else if (object instanceof String) {
               String msg = (String) object;
               PlayerMessage playerMessage = new PlayerMessage(session.getPlayer(), msg);
               server.sendObjectToSessions(playerMessage);
               return;
            } else if (object instanceof PlayerStartup) {
               session.startupPlayer((PlayerStartup) object);
               return;
            } else if (object instanceof Player) {
               Player player = (Player) object;            
               if ((game.setPlayerLocation(player, player.getLoc())) != null) {
                  server.sendObjectToSessions(game.getGameState());
               } else {
                  session.sendObject(new ErrorMessage("You cannot walk on a wall."));
               }
               return;
            }
            Logger.write("Wrong type of object send to ObjectManager (" + object.getClass() + ").");
         } else {
            Logger.write("Object sent to ObjectManager does not implement java.io.Serializable (" + object.getClass() + ").");
         }
      }
      }
      
   
}
